package com.itheima.controller.impl;

import com.itheima.controller.ITeacherController;
import com.itheima.domain.Teacher;
import com.itheima.factory.Factory;
import com.itheima.service.ITeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherControllerImpl implements ITeacherController {
    private ITeacherService TeacherService = Factory.geiTeaService();
    private Scanner sc = new Scanner(System.in);
    @Override
    public void start() {
        loop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("回到主菜单");
                    return;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    @Override
    public void addTeacher() {
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            boolean isExists = TeacherService.isExists(sid);
            if (isExists) {
                System.out.println("已经存在请重新输入");
            } else {
                break;
            }
        }
        Teacher Teacher = getTeacherInfo(sid);
        TeacherService.addTeacher(Teacher);
        System.out.println("新增成功");
    }

    @Override
    public void deleteTeacher() {
        String sid = getSid();
        TeacherService.deleteTeacher(sid);
        System.out.println("删除成功");
    }

    @Override
    public void updateTeacher() {
        String sid = getSid();
        Teacher Teacher = getTeacherInfo(sid);
        TeacherService.updateTeacher(sid, Teacher);
        System.out.println("修改成功");
    }

    private Teacher getTeacherInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher(sid, name, age, birthday);
        return teacher;
    }

    @Override
    public void findAllTeachers() {
        ArrayList<Teacher> list = TeacherService.findAllTeachers();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据,请先添加");
            return ;
        }

        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Teacher Teacher = list.get(i);
            System.out.println(Teacher.getSid() + "\t" + Teacher.getName() + "\t" + Teacher.getAge()
                    + "岁\t" + Teacher.getBirthday());
        }
    }

    private String getSid() {
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            boolean exists = TeacherService.isExists(sid);
            if (exists) {
                break;
            } else { 
                System.out.println("不存在,请重新录入");
            }
        }
        return sid;
    }
}

